<?php
/**
 * Panx 活动管理
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/7/24
 * @copyright PanxSoft Inc.
 */
namespace App\Models\Panx;

use App\Models\Concerns\ScopeFilterable;
use App\Observers\BosCleanObserver;
use App\Services\Utils\Bosable;
use App\Services\Utils\Guard;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Activity
 *
 * @property int id
 * @property Carbon|null ended_at
 * @property Carbon|null started_at
 *
 * @property-read int|null $ended_time
 * @property-read int|null $start_time
 *
 * @package App\Models\Panx
 * @mixin Builder
 */
class Activity extends Model
{
    use Bosable,
        ScopeFilterable;

    protected $key_bos_key_prefix = 'activity/picture/';

    const ACTIVITY_URL = '%s/wx-system/?store_id=%s#/%s'; // 活动首页url

    const ID_BONUS = 1; // 红包活动

    protected $table = 'activities';
    protected $guarded = [];
    protected $appends = ['started_time', 'ended_time'];

    public $bos_columns = ['activity_picture'];
    /**
     * @param $activity_picture
     * @throws \Exception
     */
    public function setActivityPictureAttribute($activity_picture)
    {
        if (is_file($activity_picture)) {
            $this->attributes['activity_picture'] = $this->uploadPostFile($activity_picture , $this->key_bos_key_prefix);
        }else{
            $this->attributes['activity_picture'] = $activity_picture;
        }

    }

    /**
     * @return mixed|null
     */
    public function getActivityPictureAttribute()
    {
        if (! $this->attributes['activity_picture']) return null;

        return $this->getStaticUrl($this->attributes['activity_picture']);
    }

    /**
     * @return null|string
     */
    public function getActivityUrlAttribute()
    {
        if (! $this->attributes['activity_url']) return null;

        $store_id = Guard::deviceAccount()->store_id ?? null;
        if ($store_id) {
            return sprintf(
                self::ACTIVITY_URL,
                config('app.url'), $store_id, trim($this->attributes['activity_url'], '/')
            );
        } else {
            return $this->attributes['activity_url'];
        }
    }

    /**
     * @used-by $started_time - 安卓使用 （为什么安卓不自己转换？）
     *
     * @return null
     */
    public function getStartedTimeAttribute()
    {
        if (! $this->attributes['started_at']) return null;

        return Carbon::parse($this->attributes['started_at'])->timestamp * 1000; // 安卓解析的是毫秒
    }

    /**
     * @used-by $ended_time - 安卓使用 （为什么安卓不自己转换？）
     *
     * @return null
     */
    public function getEndedTimeAttribute()
    {
        if (! $this->attributes['ended_at']) return null;

        return Carbon::parse($this->attributes['ended_at'])->timestamp * 1000; // 安卓解析的是毫秒
    }

    /**
     * 注册删除 bosKey 事件绑定
     */
    protected static function boot()
    {
        parent::boot();

        self::deleting(self::preventDeleteBonus());
        /**
         * 监听bos清理事件
         *
         */
        self::observe(BosCleanObserver::class);
    }

    /**
     * @test
     *
     * @return \Closure
     */
    private static function preventDeleteBonus(): \Closure
    {
        return function (Activity $self): bool {
            return $self->id !== self::ID_BONUS;
        };
    }
}
